package com.cyandpos.pad.utils;

import android.app.Activity;
import android.text.TextUtils;

import com.cyandpos.pad.common.ManagerCardManager.MangerCardData;
import com.cyandpos.pad.common.ManagerCardManager.SwipManagerCardModel;
import com.cyandpos.pad.common.commondata.BaseResponse;
import com.cyandpos.pad.common.commondata.ErpCommonData;
import com.cyandpos.pad.common.netmanager.PosHttpManager;
import com.cyandpos.pad.http.OkHttpUtils;
import com.cyandpos.pad.posdata.Permission;
import com.cyandpos.pad.posdata.SktInfo;
import com.cyandpos.pad.posdata.Sktconfig;
import com.cyandpos.pad.utils.view.InputNumberDialogCommon;
import com.google.gson.JsonObject;

/**
 * 款台权限和人员权限
 */

public class PermissionsUtils {
    public static final String XSQX = "833";//销售
    public static final String SPZK = "809";//商品折扣权限
    public static final String XSTH = "810";//销售退货权限 基础版默认810
    public static final String BANK = "847";//银行功能
    public static final String REPRINT = "814";//补打小票
    public static final String CANCEL = "816";//取消交易
    public static final String REPORT = "805";//805班报 806日报
    public static final String PASSWORD = "812";//更改口令


    public static final String HYLX = "9";//会员显示类型
    public static final String GLKLX = "6";//管理卡显示类型
    public static final String MainLX = "10";//主界面显示类型
    public static final String FSXS = "7";//负数销售
    public static  final String MSG = "该人员没有此权限,请检查人员权限配置!";


    public static final int MORE_TH = 1;//1退货
    public static final int MORE_ZK = 2;//折扣
    /**
     *
     * @param activity
     * @param permissionListener  回调
     */
    public static void checkPermission(String orderNo,Activity activity,String strFlag,final PermissionListener permissionListener){
        boolean isPermission = false;
        SktInfo sktInfo =null;
        if(ErpCommonData.loadConfig.getPermissionsList()!=null&& ErpCommonData.loadConfig.getPermissionsList().size()>0){
            isPermission = true;
            sktInfo = new SktInfo();
        }else {
            if(ErpCommonData.loadConfig.getSkt()!=null){
                sktInfo = ErpCommonData.loadConfig.getSkt();
            }else {
                sktInfo = new SktInfo();
            }

        }
        int typeFlag = 0;
        switch (strFlag){
            case XSTH:
                typeFlag = sktInfo.getPermit_th();
                break;
            case SPZK:
                typeFlag = sktInfo.getPermit_zk();
                break;
        }
        if(isPermission){
            for(Permission p: ErpCommonData.loadConfig.getPermissionsList()){
                if(p.getMsgId().equals(strFlag)){
                    if(p.getValue().equals("0")){
                        permissionListener.onPermissionFaild(MSG);
                    }else if(p.getValue().equals("1")){
                        permissionListener.onPermissionSuccess(null,0);
                    }else if(p.getValue().equals("4")){
                        if ("0".equals(ErpCommonData.managerSwipe)){
                            SwipManagerCardModel swipManagerCardModel = new SwipManagerCardModel(orderNo,activity, new SwipManagerCardModel.OnResultGLK() {
                                @Override
                                public void onResult(boolean isSuccess, Object data) {
                                    if(isSuccess){
                                        MangerCardData mangerCardData = (MangerCardData) data;
                                        permissionListener.onPermissionSuccess(mangerCardData,1);
                                    }else {
                                        permissionListener.onPermissionFaild(data.toString());
                                    }
                                }
                            });
                            swipManagerCardModel.swipe();
                        }else {
                            showDialogs(orderNo,activity,permissionListener);
                        }
                    }else if(p.getValue().equals("5")){
                        showDialogs(orderNo,activity,permissionListener);
                    }else {
                        permissionListener.onPermissionFaild(MSG);
                    }
                }

            }
        }else {
            switch (typeFlag){
                case 0:
                    permissionListener.onPermissionSuccess(null,0);
                    break;
                case 1:
                    permissionListener.onPermissionFaild(MSG);
                    break;
                case 2:
                    SwipManagerCardModel swipManagerCardModel = new SwipManagerCardModel(orderNo,activity, new SwipManagerCardModel.OnResultGLK() {
                        @Override
                        public void onResult(boolean isSuccess, Object data) {
                            if(isSuccess){
                                MangerCardData mangerCardData = (MangerCardData) data;
                                permissionListener.onPermissionSuccess(mangerCardData,1);
                            }else {
                                permissionListener.onPermissionFaild(data.toString());
                            }
                        }
                    });
                    swipManagerCardModel.swipe();
                    break;
            }

        }

    }

    /**
     *
     * @param code 种类1 会员 2主界面
     * @param id 类型
     * @param permissionListener
     */
    public static void visPermission(String orderNo,Activity activity,int code,String id,final PermissionListener permissionListener){
        boolean isPermission = false;
        SktInfo sktInfo =null;
        if(ErpCommonData.loadConfig.getSkt()!=null){
            isPermission = true;
            sktInfo = ErpCommonData.loadConfig.getSkt();
        }else {
            sktInfo = new SktInfo();
            return;
        }
        int typeFlag = 0;
        String strFlag = "";
        switch (code){
            case 1:
                strFlag = HYLX;
                break;
            case 2:
                strFlag = MainLX;
                break;
            case 3:
                strFlag= FSXS;
                break;
            case 6:
                strFlag= GLKLX;
                break;
        }
        if(isPermission){
            for (Sktconfig list: sktInfo.getSktconfigList()) {
                if (strFlag.equals(list.getLx()) && id.equals(list.getId())){
                    if (code == 6){
                        if ("0".equals(list.getValue())){
                            SwipManagerCardModel swipManagerCardModel = new SwipManagerCardModel(orderNo,activity, new SwipManagerCardModel.OnResultGLK() {
                                @Override
                                public void onResult(boolean isSuccess, Object data) {
                                    if(isSuccess){
                                        MangerCardData mangerCardData = (MangerCardData) data;
                                        permissionListener.onPermissionSuccess(mangerCardData,1);
                                    }else {
                                        permissionListener.onPermissionFaild(data.toString());
                                    }
                                }
                            });
                            swipManagerCardModel.swipe();
                        }else {
                            showDialogs(orderNo,activity,permissionListener);
                        }
                    }else {
                        String value = list.getValue();
                        MangerCardData mangerCardData = new MangerCardData();
                        mangerCardData.setName(value);
                        permissionListener.onPermissionSuccess(mangerCardData,1);
                    }

                }
            }
        }else {
            permissionListener.onPermissionFaild(MSG);
        }

    }



    /**
     * 权限选择成功回调
     */
    public interface PermissionListener{
        /**
         *
         * @param mangerCardData 管理卡内容
         * @param flag  0代表mangerCardData为null,1代表mangerCardData有值
         */
        void onPermissionSuccess(MangerCardData mangerCardData, int flag);
        void onPermissionFaild(String msg);
    }

    private static void showDialogs(final String orderNo, Activity activity, final PermissionListener permissionListener) {


        InputNumberDialogCommon inputNumberDialogCommon= InputNumberDialogCommon.getInputNumberDialogCommon(activity, new InputNumberDialogCommon.InputDialogOnSubmitClickListener() {
            @Override
            public void InputDialogOnSubmitClick(String content) {
                if(content.equals("") || content==null){
                    ToastUtils.showMsg( "卡号不能为空");
                    return;
                }
                PosHttpManager.managerYZHttpEexcute(orderNo,content, new OkHttpUtils.ResultCallback() {
                    @Override
                    public void onSuccess(JsonObject response) {
                        BaseResponse<MangerCardData> baseResponse = BaseResponse.fromJson(response.toString(),MangerCardData.class);
                        if(baseResponse.getCode().equals("0")){
                            MangerCardData mangerCardData = new MangerCardData();
                            mangerCardData.setName(baseResponse.getData().getName());
                            mangerCardData.setCardNo(baseResponse.getData().getCardNo());
                            permissionListener.onPermissionSuccess(mangerCardData,1);
                        }else{
                            if (TextUtils.isEmpty(baseResponse.getTips())){
                                permissionListener.onPermissionFaild(baseResponse.getMsg());
                            }else {
                                permissionListener.onPermissionFaild(baseResponse.getTips());
                            }
                        }
                    }

                    @Override
                    public void onFailure(String msg) {
                        permissionListener.onPermissionFaild(msg);
                    }
                });
            }
        });
        inputNumberDialogCommon.setIsCanInputPoint(false);
        inputNumberDialogCommon.setTitle("管理卡验证");
        inputNumberDialogCommon.setContent("请输入卡号");
        inputNumberDialogCommon.setEtHint("请输入卡号");
        inputNumberDialogCommon.showDialog(false);

    }

}
